#pragma once
// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: Frame.h
// Description: Frame Class
//      Author: Ziming Li
//     Created: 2021-07-13
// -------------------------------------------------
// Copyright (C) 2021 MingYang Software Technology.
// -------------------------------------------------

/*
 * Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
 *                     1999-2001 Lars Knoll <knoll@kde.org>
 *                     1999-2001 Antti Koivisto <koivisto@kde.org>
 *                     2000-2001 Simon Hausmann <hausmann@kde.org>
 *                     2000-2001 Dirk Mueller <mueller@kde.org>
 *                     2000 Stefan Schimanski <1Stein@gmx.de>
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef Frame_h
#define Frame_h

#if 0 // BKTODO:
#include "core/frame/FrameTypes.h"
#include "core/loader/FrameLoaderTypes.h"
#include "core/page/FrameTree.h"
#endif
#include "blinkit/blink/renderer/platform/heap/Handle.h"
#include "blinkit/blink/renderer/wtf/Forward.h"
#include "blinkit/blink/renderer/wtf/RefCounted.h"

namespace blink {

class DOMWindow;
class DOMWrapperWorld;
class Document;
class FrameClient;
class HTMLFrameOwnerElement;
class KURL;
class SecurityContext;
// BKTODO: class Settings;
class WindowProxy;
class WindowProxyManager;
struct FrameLoadRequest;
#ifdef BLINKIT_UI_ENABLED
class ChromeClient;
class FrameHost;
class LayoutPart;
class Page;
#endif

// Status of user gesture.
enum class UserGestureStatus { Active, None };

// Frame is the base class of LocalFrame and RemoteFrame and should only contain
// functionality shared between both. In particular, any method related to
// input, layout, or painting probably belongs on LocalFrame.
class Frame
{
public:
    virtual ~Frame();

    DECLARE_VIRTUAL_TRACE();

    virtual DOMWindow* domWindow() const = 0;
    virtual WindowProxy* windowProxy(DOMWrapperWorld&) = 0;

    virtual void navigate(Document& originDocument, const KURL&, bool replaceCurrentItem, UserGestureStatus) = 0;
    // This version of Frame::navigate assumes the resulting navigation is not
    // to be started on a timer. Use the method above in such cases.
    virtual void navigate(const FrameLoadRequest&) = 0;
    // BKTODO: virtual void reload(FrameLoadType, ClientRedirectPolicy) = 0;

    virtual void detach(void);
    // BKTODO: void detachChildren();
    virtual void disconnectOwnerElement();
#ifdef BLINKIT_UI_ENABLED
    virtual bool shouldClose() = 0;
#endif

    FrameClient* client() const;

#ifdef BLINKIT_UI_ENABLED
    // NOTE: Page is moving out of Blink up into the browser process as
    // part of the site-isolation (out of process iframes) work.
    // FrameHost should be used instead where possible.
    Page* page() const;
    FrameHost* host() const; // Null when the frame is detached.
#endif

#if 0 // BKTODO:
    FrameOwner* owner() const;
    void setOwner(FrameOwner* owner) { m_owner = owner; }
    HTMLFrameOwnerElement* deprecatedLocalOwner() const;
#endif

    // BKTODO: FrameTree& tree() const;
#ifdef BLINKIT_UI_ENABLED
    ChromeClient& chromeClient() const;
#endif

    // BKTODO: virtual SecurityContext* securityContext() const = 0;

    Frame* findFrameForNavigation(const AtomicString& name, Frame& activeFrame);
    Frame* findUnsafeParentScrollPropagationBoundary();

    // This prepares the Frame for the next commit. It will detach children,
    // dispatch unload events, abort XHR requests and detach the document.
    // Returns true if the frame is ready to receive the next commit, or false
    // otherwise.
    virtual bool prepareForCommit() = 0;

    bool canNavigate(const Frame&);
    virtual void printNavigationErrorMessage(const Frame&, const char* reason) = 0;

#ifdef BLINKIT_UI_ENABLED
    LayoutPart* ownerLayoutObject() const; // LayoutObject for the element that contains this frame.
#endif

    int64_t frameID() const { return m_frameID; }

    // BKTODO: Settings* settings() const; // can be null

    // isLoading() is true when the embedder should think a load is in progress.
    // In the case of LocalFrames, it means that the frame has sent a didStartLoading()
    // callback, but not the matching didStopLoading(). Inside blink, you probably
    // want Document::loadEventFinished() instead.
    void setIsLoading(bool isLoading) { m_isLoading = isLoading; }
    bool isLoading() const { return m_isLoading; }

    virtual WindowProxyManager* windowProxyManager() const = 0;

    /**
     * BlinKit Placeholders
     */
    constexpr bool isMainFrame(void) const { return true; }
    constexpr bool isLocalFrame(void) const { return true; }
    constexpr bool isLocalRoot(void) const { return true; }
    constexpr bool isRemoteFrame(void) const { return false; }
protected:
#ifdef BLINKIT_UI_ENABLED
    Frame(FrameClient*, FrameHost*);
#else
    Frame(FrameClient*);
#endif

    // BKTODO: mutable FrameTree m_treeNode;

#ifdef BLINKIT_UI_ENABLED
    RawPtrWillBeMember<FrameHost> m_host;
#endif
    // BKTODO: RawPtrWillBeMember<FrameOwner> m_owner;

private:
    RawPtrWillBeMember<FrameClient> m_client;
    // Needed to identify Frame Timing requests.
    int64_t m_frameID;
    bool m_isLoading = false;
};

inline FrameClient* Frame::client() const
{
    return m_client;
}

#if 0 // BKTODO:
inline FrameOwner* Frame::owner() const
{
    return m_owner;
}

inline FrameTree& Frame::tree() const
{
    return m_treeNode;
}
#endif

// Allow equality comparisons of Frames by reference or pointer, interchangeably.
DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES_REFCOUNTED(Frame)

} // namespace blink

#endif // Frame_h
